<?php
class Upload extends CI_Controller{
	
	function __construct()
	{
		parent::__construct();
		$this->load->library("upload");
		$this->load->helper("form");
	}
	
	function index()
	{
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			$file				= 'video_file';
			$config['upload_path']		= 'D:/VertrigoServ/www/demo/ci/video_folder/';
			$config['allowed_types'] 	= '*';
			$config['max_size']		= '50000';
			$config['max_width']  		= '';
			$config['max_height']  		= '';
			
			$this->upload->initialize($config);
			//$this->load->library('upload', $config);
			
			if(!$this->upload->do_upload($file))
			{
				// If there is any error
				$err_msgs .= 'Error in Uploading video '.$this->upload->display_errors().'';
				echo $err_msgs;
			}
			else
			{
				$data=array('upload_data' => $this->upload->data());
				$video_path = $data['upload_data']['file_name'];
			
				$directory_path 	 = $data['upload_data']['file_path'];
				$directory_path_full      = $data['upload_data']['full_path'];
				$file_name 		= $data['upload_data']['raw_name'];
			
				// ffmpeg command to convert video
			
				exec("ffmpeg -i ".$directory_path_full." ".$directory_path.$file_name.".flv");
				// $file_name is same file name that is being uploaded but you can give your custom video name after converting So use something like myfile.flv.
			
				/// In the end update video name in DB
				$array = array(
						'video' => $file_name.'.'.'flv',
				);
				//$this->db->set($array);
				//$this->db->where('id',$id); // Table where you put video name
				//$query = $this->db->update('user_videos');
			}
		}
		$this->load->view("upload/index");
	}
}